package ExceptionAndIO.IO;

import OOP.Rectangle;
import org.testng.annotations.Test;

import java.io.*;

public class IODemo {
    static PrintStream originaOut=System.out;
    public static void main(String[]arg)  {
        OutputStream outputStream= null;
        String loc="JavaSE/ExceptionAndIO/IO/test.txt";
        File file=new File(loc);
        try {
            outputStream = new FileOutputStream(file,true);//注意第二个参数
        } catch (FileNotFoundException e) {
            System.err.println("找不到文件");
        }
        PrintStream printStream=new PrintStream(outputStream);
        System.setOut(printStream);
        System.out.println("这是一个测试");
        System.setOut(originaOut);
        System.out.println("重新输出");
    }

    @Test
    public void test1(){
        try {
            FileReader fileReader=new FileReader("JavaSE/ExceptionAndIO/IO/io.txt");

            int ch1=fileReader.read();
            System.out.println((char)ch1);
            int ch2=fileReader.read();
            System.out.println((char)ch2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void test2(){
        try{
            FileReader fileReader=new FileReader("JavaSE/ExceptionAndIO/IO/io.txt");
            int ch=-1;
            while((ch=fileReader.read())!=-1){
                System.out.println((char)ch);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void test3() {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("io.txt");
            char[] buffer = new char[10];
            int length = -1;
            while ((length = fileReader.read(buffer)) != -1) {
                System.out.println(length);
                System.out.println(buffer);
            }
            System.out.println(length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void test4() {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            fileReader = new FileReader("JavaSE/ExceptionAndIO/IO/io.txt");
            fileWriter = new FileWriter("JavaSE/ExceptionAndIO/IO/io_back.txt");
            char[] buffer = new char[10];
            int length = -1;
            while ((length = fileReader.read(buffer)) != -1) {
                System.out.println(length);
                System.out.println(buffer);
                fileWriter.write(buffer, 0, length);
            }
            System.out.println(length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void testFileInputStreamFileOutputStram() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream("JavaSE/ExceptionAndIO/IO/bd.png");
            fileOutputStream = new FileOutputStream("JavaSE/ExceptionAndIO/IO/bd_back.png");
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void testObjectInputStream() {
        Rectangle rect=new Rectangle();

        ObjectInputStream objectInputStream = null;
        ObjectOutputStream objectOutputStream=null;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream=null;
        try{
            fileOutputStream=new FileOutputStream("JavaSE/ExceptionAndIO/IO/rec");
            objectOutputStream=new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(rect);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            fileInputStream = new FileInputStream("JavaSE/ExceptionAndIO/IO/rec");
            objectInputStream = new ObjectInputStream(fileInputStream);
            Rectangle rectangle = (Rectangle) objectInputStream.readObject();
            System.out.println(rectangle);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
